﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ComeSeeThis_MVC2.Models
{

    public class GearingScript
    {
        public string TheScript { get; private set; }
        public string Variance { get; private set; }

        public GearingScript()
        {
            Variance = "3";
            const string returnChar = "<br>";

            TheScript = "var Variance = " + Variance + ";" + returnChar + returnChar;

            //TheScript += MotorCompanies() + returnChar + returnChar;
            //TheScript += VehicleTypes() + returnChar + returnChar;
            TheScript += PopulateVehicleDropDown(returnChar) + returnChar + returnChar;
        }

        private static string PopulateVehicleDropDown(string returnChar)
        {
            var vehicles = new VehicleRepository().GetAllVehicles(true);

            var theScript = "function PopulateVehicleDropDown() {" + returnChar;
            //theScript += "    for (var i = 0; i < " + vehicles.Count() + "; i++) {" + returnChar;

            foreach (var vehicle in vehicles)
            {
                theScript += "document.getElementById(\"VehicleId\").options[i] = new Option(\"" + vehicle.Company.CompanyName + " " + vehicle.VehicleName + "\"," + vehicle.VehicleId + ")" + returnChar;
            }
            theScript += "}" + returnChar;
            return theScript;
        }

        private static string MotorCompanies()
        {
            var list = new CompanyRepository().GetAllCompanies(1, ProductType.Motor, true).ToList();

            var theScript = "MotorBrands = new Array(";
            var first = false;
            foreach (var motorCompany in list)
            {
                if (first) theScript += ", ";
                theScript += "\"" + motorCompany.CompanyName + "\"";
                first = true;

            }

            theScript += ");";

            return theScript;
        }
        private static string VehicleTypes()
        {
            var list = new VehicleTypeRepository().GetAllVehicleTypes().ToList();

            var theScript = "VehicleTypes = new Array(";

            var first = true;
            foreach (var item in list)
            {
                if (!first) theScript += ", ";
                theScript += "\"" + "1/" + item.Scale + " " + item.VehicleTypeName + "\"";
                first = false;
            }

            theScript += ");";

            return theScript;
        }
        private static string Vehicles()
        {
            var list = new VehicleRepository().GetAllVehicles(true).ToList();

            var theScript = "Vehicles = new Array(";

            var first = true;
            foreach (var vehicle in list)
            {
                if (!first) theScript += ",<br> ";
                theScript += "Array(";
                theScript += "\"" + vehicle.Company.CompanyName + "\", \"" + vehicle.VehicleName + "\", \"" +
                             vehicle.TransmissionRatio + "\", \"" + vehicle.VehicleTypeId + "\", ";

                theScript += "Array(\"";
                var i = 1;
                foreach (var vehicleSpur in vehicle.VehicleSpurs)
                {
                    if (i < vehicle.VehicleSpurs.Count) theScript += "\", \"";
                    theScript += vehicleSpur.SpurGear;
                    i++;
                }
                theScript += "\")";
                first = false;
            }

            theScript += ");";

            return theScript;
        }
    }
}